package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.xcity.db.entity.Player;
import com.xcity.db.entity.Ranking;
import com.xcity.db.entity.Relation2;
import com.xcity.game.buff.Buff;
import com.xcity.game.player.PlayerUtils;
import com.xcity.game.rank.RankingList;
import com.xcity.game.rank.RankingListEnum;
import com.xcity.game.relation.Relations;
import com.xcity.game.worldevent.WorldEventService;
import com.xcity.pomelo.push.msg.BuffsPush.BuffInfo;

import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;
import naga.x.util.CommonUtils;

@PomeloMessage(type = Message.Type.RESPONSE)
public class FriendListResponse extends Response {

	public static class Actor {
		public long roleId;
		public String roleName;
		public String img;
		public int lv;
		public byte sex;
		@Deprecated
		public String sign;
		public boolean onLine;
		public int ranking;
		@Deprecated
		public boolean isNotify;
		@Deprecated
		public String chatTxt;
		@Deprecated
		public List<BuffInfo> buffs;
		public int intimacy; // 亲密度
		public int charm; // 魅力值
		public byte canTapLike; // 是否可以给该玩家点赞
		public byte canPresentStr; // 是否可赠送体力给该好友
	}
	
	private List<Actor> friends;
	private int num; // 当前拥有的好友数量
	private int max; // 最多可拥有的好友数量
	
	public FriendListResponse(int serial, Player owner, Relations rels, List<Player> actors) {
		super(serial);
		int size = actors.size();
		List<Actor> friends = new ArrayList<Actor>(size);
		for (int i = 0; i < size; i++) {
			Player actor = actors.get(i);
			Actor f = new Actor();
			f.roleId = actor.getId();
			f.roleName = actor.getName();
			f.img = actor.getIcon();
			f.lv = actor.getLevel();
			f.sex = actor.getSex();
			f.sign = actor.getSign();
			f.onLine = actor.isOnline();
//			Session sess = actor.getSession();
			RankingList rl = RankingListEnum.ASSETS.instance;
			Ranking rank = rl.findEntry(actor);
			f.ranking = rank != null ? rank.getIndex() + 1 : rl.getCapacity() + CommonUtils.randomInt(10, 100);
			if (rels != null) {
				Relation2 rel = rels.find(Relation2.TYPE_FRIEND, actor.getId());
				f.intimacy = rel != null ? rel.getIntimacy() : 0;
				f.charm = PlayerUtils.getCharm(actor);
			}
			if (actor.getBuffs() != null && actor.getBuffs().getBuffs().size() > 0) {
				List<Buff> buffs = actor.getBuffs().getBuffs();
				f.buffs = new ArrayList<BuffInfo>(buffs.size());
				for (int j = 0, n = buffs.size(); j < n; j++) {
					Buff buff = buffs.get(j);
					if (buff.getId() > WorldEventService.buffIdOffset) {
						BuffInfo bi = new BuffInfo(buff);
						f.buffs.add(bi);
					}
				}
			} else {
				f.buffs = Collections.emptyList();
			}
			friends.add(f);
			f.canTapLike = (byte) (PlayerUtils.getLeftTapLikeTimes(owner) > 0 && !PlayerUtils.wasTappedLike(owner, actor.getId()) ? 1 : 0);
			f.canPresentStr = (byte) (PlayerUtils.wasPresentedStrength(owner, actor.getId()) ? 0 : 1);
		}
		this.friends = friends;
		this.num = size;
		this.max = PlayerUtils.getFriendsNumMax(owner);
	}

	public List<Actor> getFriends() {
		return friends;
	}

	public void setFriends(List<Actor> actors) {
		this.friends = actors;
	}

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

	public int getMax() {
		return max;
	}

	public void setMax(int max) {
		this.max = max;
	}
	
}
